package com.example.demo.jvm;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: xiatian
 * @CreateTime: 2021/11/17 5:28 下午
 * @Description:
 */
@RestController
@RequestMapping("/thread")
public class ThreadSwapTest {

    @RequestMapping(value = "/swap", method = RequestMethod.GET)
    public void ThreadSwapTest(int num) {
        System.out.println("模拟线程切换");
        for (int i = 0; i < num; i++) {
            new Thread(new ThreadS(new AtomicInteger(0)), "thread-swap" + i).start();
        }
    }

    private class ThreadS implements Runnable {
        private AtomicInteger integer;

        public ThreadS(AtomicInteger integer) {
            this.integer = integer;
        }

        @Override
        public void run() {
            while (true) {
                integer.addAndGet(1);
                Thread.yield();
            }
        }
    }


}
